package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.BatchQueryAchievementTypeByEntityReq;
import com.sg.dto.base.app.req.QueryAchievementTypeDetailReq;
import com.sg.dto.base.app.req.QueryAchievementTypeListReq;
import com.sg.entity.OmsAchievementType;
import com.sg.service.base.app.MOmsAchievementTypeService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsAchievementTypeMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsAchievementTypeServiceImpl
        extends ServiceImpl<OmsAchievementTypeMapper, OmsAchievementType>
        implements MOmsAchievementTypeService {

    @Autowired
    OmsAchievementTypeMapper omsAchievementTypeMapper;

    /**
     * 3-10-02新增成果类型[9098]
     * gen by moon at 4/3/2024, 1:42:51 PM
     */
    @Trace(operationName = "3-10-02新增成果类型")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addAchievementType(OmsAchievementType omsAchievementType) {
        omsAchievementType.setAchievementTypeId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsAchievementType.setIsArchive("FALSE");
        omsAchievementType.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsAchievementType.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsAchievementType != null && omsAchievementType.getSubjectLifeCycle() != null && omsAchievementType.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementType.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsAchievementType.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsAchievementType.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAchievementType != null && omsAchievementType.getSubjectLifeCycle() != null && omsAchievementType.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementType.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsAchievementType.setIsValid("FALSE");
        if (super.save(omsAchievementType)) {
            return omsAchievementType.getAchievementTypeId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-10-02查询成果类型列表[9101]
     * gen by moon at 4/3/2024, 6:42:41 PM
     */
    @Trace(operationName = "3-10-02查询成果类型列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAchievementType> queryAchievementTypeList(QueryAchievementTypeListReq reqDto) {

        try {
            QueryWrapper<OmsAchievementType> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAchievementType.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAchievementType.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAchievementType.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsAchievementType>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-10-02查询成果类型详情[9102]
     * gen by moon at 4/3/2024, 1:42:58 PM
     */
    @Trace(operationName = "3-10-02查询成果类型详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsAchievementType queryAchievementTypeDetail(QueryAchievementTypeDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsAchievementType> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsAchievementType> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsAchievementType retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-10-02修改成果类型[9099]
     * gen by moon at 4/3/2024, 6:42:39 PM
     */
    @Trace(operationName = "3-10-02修改成果类型")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAchievementType(OmsAchievementType omsAchievementType) {
        omsAchievementType.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsAchievementType != null && omsAchievementType.getSubjectLifeCycle() != null && omsAchievementType.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementType.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsAchievementType != null && omsAchievementType.getSubjectLifeCycle() != null && omsAchievementType.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAchievementType.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsAchievementType.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAchievementType != null && omsAchievementType.getSubjectLifeCycle() != null && omsAchievementType.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementType.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsAchievementType != null && omsAchievementType.getSubjectLifeCycle() != null && omsAchievementType.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAchievementType.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsAchievementType);
    }

    /**
     * 3-10-02删除成果类型[9100]
     * gen by moon at 4/3/2024, 6:42:40 PM
     */
    @Trace(operationName = "3-10-02删除成果类型")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteAchievementType(String achievementTypeId) {

        return super.removeById(achievementTypeId);
    }

    /**
     * 3-10-02批量查询成果类型By主键[9267]
     * gen by moon at 4/5/2024, 8:58:28 PM
     */
    @Trace(operationName = "3-10-02批量查询成果类型By主键")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAchievementType> batchQueryAchievementTypeByEntity(BatchQueryAchievementTypeByEntityReq reqDto) {

        try {
            QueryWrapper<OmsAchievementType> queryWrapper = new QueryWrapper<>();

            if (reqDto.getAchievementTypeList() == null || reqDto.getAchievementTypeList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("achievement_type_id", reqDto.getAchievementTypeList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAchievementType.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAchievementType.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAchievementType.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsAchievementType>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
